﻿Imports System.Data
Imports PadovadoAPI

Partial Class EventiDaPrenotare
    Inherits System.Web.UI.Page

    Public Sub CaricaRepeater()
        Dim FK_Target As Integer = 5
        If Not String.IsNullOrWhiteSpace(Request.QueryString("FK_Target")) Then
            FK_Target = Request.QueryString("FK_Target")
        End If
        Dim CurrentDate As DateTime = DateTime.Now.Date()
        Dim dbConnector As New DBObject
        Dim sql As String = ""
        sql = "SELECT isnull(I.Comune,'') + ' ' + convert(varchar,E.DataInizio,103) + ' ' +  E.NomeEvento as NomeEvento, E.Descrizione as DescrizioneEvento, " & _
            " CASE isnull(E.imgEvento, '') WHEN '' then MAX(T.TargetImgURLSmall) else E.imgEvento end as ImgEvento, " & _
            " convert(varchar,E.DataInizio,103) as DataInizio, convert(varchar,E.DataFine,103) as DataFine, E.Link, E.Brochure, " & _
             " CASE isnull(E.link, '') WHEN '' then 0 else 1 end as LinkPresente, " & _
            " CASE isnull(E.Brochure, '') WHEN '' then 0 else 1 end as BrochurePresente, " & _
                 " isnull(I.Strada,'') + ' ' + isnull(I.Civico,'') + ' ' + isnull(I.Comune,'') + ' ' + isnull(I.Presso,'') as Indirizzo, " & _
                   " MAX(T.TargetImgURLSmall), " & _
                   " A.Denominazione as Organizzatore, " & _
                    " CASE isnull(E.intCategoria, 100) " & _
                      "     WHEN 100 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Generico/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 1 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Concerto/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 2 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Festa/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 3 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Discoteca/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 4 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Fiera/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 5 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Sagra/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 6 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Gita/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50))" & _
                      "     WHEN 7 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Gratis/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) " & _
                      "     WHEN 8 then 'http://www.padovado.it/pd-ristorante-festa-donne-musica-locali-eventi-stasera-veneto/Mostra/PaginaEvento.aspx?PK_Evento=' + cast(E.PK_Evento as varchar(50)) end as LinkApri, " & _
                   " convert(varchar,ISC.DataApertura,103) as DataApertura, convert(varchar,ISC.DataChiusura,103) as DataChiusura, ISC.Dettagli, " & _
                   " ISC.ModuloIscrizione as LinkIscrizione, " & _
                   " CASE isnull(ISC.ModuloIscrizione, '') WHEN '' then 0 else 1 end as ModuloIscrizionePresente, " & _
                   " ROW_NUMBER() OVER(ORDER BY E.DataInizio) AS 'RowNumber'" & _
                   " FROM Evento E " & _
                   " INNER JOIN Iscrizione ISC ON FK_Iscrizione = PK_Iscrizione" & _
                   " LEFT OUTER JOIN Indirizzo I ON I.PK_Indirizzo = FK_Indirizzo " & _
                   " LEFT OUTER JOIN Evento_Organizzatore EO ON PK_Evento = EO.FK_Evento " & _
                   " LEFT OUTER JOIN Organizzatore O ON O.PK_Organizzatore = EO.FK_Organizzatore " & _
                   " LEFT OUTER JOIN Anagrafica A ON A.PK_Anagrafica = O.FK_Anagrafica " & _
                   " LEFT OUTER JOIN Evento_Target ET ON ET.FK_Evento = PK_Evento " & _
                   " LEFT OUTER JOIN Target T ON T.PK_Target = ET.FK_Target " & _
                    " WHERE E.Deleted = 0  AND (ISC.DataApertura >= CAST('" & CurrentDate.AddDays(3).ToString("yyyyMMdd") & "' AS DATE)" & _
                    " OR (ISC.DataApertura <= CAST('" & CurrentDate.ToString("yyyyMMdd") & "' as date) " & _
                    " AND ISC.DataChiusura >= CAST('" & CurrentDate.ToString("yyyyMMdd") & "' as date)))"
        'DBObject.WriteLog(sql)
        ' Throw New Exception(sql)
        If (FK_Target <> 5) Then
            sql &= " AND ET.FK_target = " & FK_Target
        End If
        sql &= " GROUP BY E.NomeEvento, E.Descrizione, E.DataInizio, E.DataFine, E.Link, I.Strada, I.Civico, I.Comune, I.Presso, PK_Evento, E.imgEvento, E.Brochure, E.Priorita," & _
                 " A.Denominazione, E.intCategoria, ISC.DataApertura, ISC.DataChiusura, ISC.Dettagli, ISC.ModuloIscrizione"
        'Throw New Exception(sql)
        Dim ds As New DataSet
        ds = dbConnector.FillDS(sql, Nothing, 1)

        If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 Then
            ds.Tables(0).Columns.Add("ItemTemplateId")
            ds.Tables(0).Columns.Add("ImageApriId")
            For Each dr As DataRow In ds.Tables(0).Rows
                dr("ItemTemplateId") = "ItemTemplate_" & dr("RowNumber")
                dr("ImageApriId") = "ImageApri_" & dr("RowNumber")
            Next
            RptEventi.DataSource = ds
            RptEventi.DataBind()
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CaricaRepeater()
        End If
    End Sub
End Class
